/*************************************************************************
	Scenario Editor
	
*************************************************************************/

using UnityEditor;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class UKagScenarioPreviewer: EditorWindow
{
	private TextAsset m_ScenarioAsset = null;
	private UKagScenarioEditor.UKagScenarioForEditor m_Scenario = null;
	private string[] m_Labels = null;
	private int m_CurrLabelIdx = 0;

	[MenuItem ("UKag/UKag Scenario Previewer")]
	static void Init ()
	{
        // Get existing open window or if none, make a new one:
		EditorWindow editor = EditorWindow.GetWindow (typeof (UKagScenarioPreviewer));
		editor.minSize = new Vector2(300, 200);
		editor.title = "Scenario Previewer";
    }

	void OnSelectionChange() {
		if (Selection.activeObject is TextAsset)
		{
			UKagTagHandler.InitTagHandlers();
			m_ScenarioAsset = Selection.activeObject as TextAsset;
			m_Scenario = new UKagScenarioEditor.UKagScenarioForEditor();
			m_Scenario.LoadTextFile(m_ScenarioAsset);
			List<string> labelList = new List<string>();
			for (int i=0; i<m_Scenario.TagList.Count; i++)
				if (m_Scenario.TagList[i].name == "label")
					labelList.Add(m_Scenario.TagList[i].GetParamValue("text"));
			m_Labels = labelList.ToArray();
		}
		else
		{
			m_ScenarioAsset = null;
			m_Scenario = null;
			m_Labels = null;
		}
		m_CurrLabelIdx = 0;
		Repaint();
	}

	void OnGUI ()
	{
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.ObjectField("Scenario: ", m_ScenarioAsset, typeof(TextAsset), false);
		if (Application.isPlaying && UKagSystem.Instance != null && m_ScenarioAsset != null)
			if (GUILayout.Button("Load"))
				UKagSystem.Instance.conductor.LoadScenarioFile(m_ScenarioAsset.name);
		EditorGUILayout.EndHorizontal();
		if (m_ScenarioAsset == null || m_Labels == null)
			return;

		EditorGUILayout.BeginHorizontal();
		m_CurrLabelIdx = EditorGUILayout.Popup("Label: ", m_CurrLabelIdx, m_Labels);
		if (Application.isPlaying && UKagSystem.Instance != null)
			if (GUILayout.Button("Jump"))
				UKagSystem.Instance.conductor.GoToLabel(m_Labels[m_CurrLabelIdx]);
		EditorGUILayout.EndHorizontal();

	}
}
